home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Chans / testchan / testchan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  11.8 KB  |  607 lines

  1. /* testchan.c: as invoked by qmgr to do benchmarking stuff*/
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Chans/testchan/RCS/testchan.c,v 6.0 1991/12/18 20:12:42 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Chans/testchan/RCS/testchan.c,v 6.0 1991/12/18 20:12:42 jpo Rel $
  9.  *
  10.  * $Log: testchan.c,v $
  11.  * Revision 6.0  1991/12/18  20:12:42  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include "head.h"
  19. #include "chan.h"
  20. #include "prm.h"
  21. #include "q.h"
  22. #include "dr.h"
  23. #include "qmgr.h"
  24. #include <pwd.h>
  25. #include "sys.file.h"
  26. #include <signal.h>
  27. #include <isode/internet.h>
  28.  
  29. extern  char    *quedfldir;
  30. extern void    rd_end(), chan_init(), err_abrt(), timer_start(), timer_end();
  31. FILE            *msg_fp;
  32. CHAN            *mychan;
  33. char            *this_msg;
  34.  
  35.  
  36. static char *files[] = {
  37.     "/usr/dict/words",
  38.     "/etc/passwd",
  39.     "/usr/adm/messages",
  40.     "/etc/motd",
  41.     "/etc/hosts",
  42.     "/etc/printcap",
  43.     "/etc/rc.local",
  44.     NULLCP
  45.     };
  46. #define RANDFILE() (files[random() % (sizeof(files)/sizeof(*files) -1)])
  47.  
  48. static int data_bytes;
  49. extern time_t time();
  50.  
  51.  
  52. static struct type_Qmgr_DeliveryStatus *process();
  53. static void dirinit();
  54. static int chaninit();
  55. static int endproc ();
  56. static int dotext ();
  57. static int copy ();
  58. static struct hostent *find_host ();
  59. static FILE *serv_open();
  60. #define OPT_RANDOM 1
  61. #define OPT_SLEEP 2
  62. #define OPT_NETECHO 3
  63. int option = OPT_RANDOM;
  64. /* ---------------------  Begin  Routines  -------------------------------- */
  65.  
  66.  
  67.  
  68.  
  69. main (argc, argv)
  70. int     argc;
  71. char    **argv;
  72. {
  73.     char    *p;
  74.     int opt;
  75.  
  76.     if ((p = rindex (argv[0], '/')) != NULLCP)
  77.         p ++;
  78.     if (p == NULLCP || *p == NULL)
  79.         p = argv[0];
  80.  
  81.     chan_init (p);   /* init the channel - and find out who we are */
  82.  
  83.     dirinit();              /* get to the right directory */
  84.     (void) signal (SIGPIPE, SIG_IGN);
  85.  
  86.     while ((opt = getopt (argc, argv, "es")) != EOF) {
  87.         switch (opt) {
  88.             case 's':
  89.             option = OPT_SLEEP;
  90.             break;
  91.             case 'e':
  92.             option = OPT_NETECHO;
  93.             break;
  94.         }
  95.     }
  96.  
  97. #ifdef PP_DEBUG
  98.     if (argc > 1 && strcmp (argv[1], "debug") == 0)
  99.         debug_channel_control (argc, argv, chaninit, process, endproc);
  100.     else
  101. #endif
  102.         channel_control (argc, argv, chaninit, process, endproc);
  103.     exit (0);
  104. }
  105.  
  106.  
  107.  
  108. static int chaninit (arg)
  109. struct type_Qmgr_Channel *arg;
  110. {
  111.     char    *p = qb2str (arg);
  112.  
  113.     if ((mychan = ch_nm2struct (p)) == (CHAN *)0)
  114.         err_abrt (RP_PARM, "Channel '%s' not known", p);
  115.  
  116.     srandom (getpid());
  117.     rename_log(p); 
  118.     PP_NOTICE (("starting %s (%s)", mychan -> ch_name, mychan -> ch_show));
  119.     free (p);
  120.     return OK;
  121. }
  122.  
  123. static int endproc ()
  124. {
  125.     PP_NOTICE (("Closing %s", mychan -> ch_name));
  126. }
  127.  
  128. static struct type_Qmgr_DeliveryStatus *process (arg)
  129. struct type_Qmgr_ProcMsg *arg;
  130. {
  131.     struct prm_vars prm;
  132.     struct type_Qmgr_UserList *up;
  133.     Q_struct        Qstruct, *qp = &Qstruct;
  134.     int     retval;
  135.     ADDR    *ap,
  136.         *ad_sendr = NULLADDR,
  137.         *ad_recip = NULLADDR,
  138.         *alp = NULLADDR,
  139.         *ad_list = NULLADDR;
  140.     int     ad_count;
  141.     char *cur_host = NULLCP;
  142.  
  143.     if (this_msg) free (this_msg);
  144.  
  145.     this_msg = qb2str (arg -> qid);
  146.  
  147.     bzero ((char *)&prm, sizeof prm);
  148.     bzero ((char *)qp, sizeof *qp);
  149.  
  150.     (void) delivery_init (arg -> users);
  151.  
  152.     retval = rd_msg (this_msg, &prm, qp, &ad_sendr, &ad_recip, &ad_count);
  153.  
  154.     if (rp_isbad (retval)) {
  155.         PP_LOG (LLOG_EXCEPTIONS, ("rd_msg err: %s", this_msg));
  156.         return delivery_setallstate (int_Qmgr_status_messageFailure,
  157.                          "Can't read message");
  158.     }
  159.  
  160.     for (ap = ad_recip; ap; ap = ap -> ad_next) {
  161.         for (up = arg ->users; up; up = up -> next) {
  162.             if (up -> RecipientId -> parm != ap -> ad_no)
  163.                 continue;
  164.  
  165.             switch (chan_acheck (ap, mychan,
  166.                          ad_list == NULLADDR, &cur_host)) {
  167.                 default:
  168.                 case NOTOK:
  169.                 continue;
  170.  
  171.                 case OK:
  172.                 break;
  173.             }
  174.             break;
  175.         }
  176.         if (up == NULL)
  177.             continue;
  178.  
  179.         if (ad_list == NULLADDR)
  180.             ad_list = alp = (ADDR *) calloc (1, sizeof *alp);
  181.         else {
  182.             alp -> ad_next = (ADDR *) calloc (1, sizeof *alp);
  183.             alp = alp -> ad_next;
  184.         }
  185.         *alp = *ap;
  186.         alp -> ad_next = NULLADDR;
  187.     }
  188.  
  189.     if (ad_list == NULLADDR) {
  190.         PP_LOG (LLOG_EXCEPTIONS, ("No recipients in user list"));
  191.         rd_end ();
  192.         q_free (qp);
  193.         return deliverystate;
  194.     }
  195.  
  196.     PP_NOTICE (("processing msg %s to %s", this_msg, cur_host));
  197.  
  198.     deliver (ad_list, qp);
  199.  
  200.     q_free (qp);
  201.     rd_end();
  202.  
  203.     return deliverystate;
  204. }
  205.  
  206. static void dirinit()       /* Change into pp queue space */
  207. {
  208.     if (chdir (quedfldir) < 0)
  209.         err_abrt (RP_LIO, "Unable to change directory to '%s'",
  210.                         quedfldir);
  211. }
  212.  
  213. deliver (ad_list, qp)
  214. ADDR    *ad_list;
  215. Q_struct *qp;
  216. {
  217.     ADDR    *ap;
  218.  
  219.     PP_TRACE (("deliver ()"));
  220.  
  221.     switch (option) {
  222.         case OPT_NETECHO:
  223.         do_echo ("/usr/dict/words");
  224.         break;
  225.         case OPT_SLEEP:
  226.         do_sleep (5);
  227.         break;
  228.         case OPT_RANDOM:
  229.         do_random ();
  230.         break;
  231.     }
  232.     if (option == OPT_RANDOM) {
  233.         if (random()% 100 == 1) {
  234.             for (ap = ad_list; ap; ap = ap -> ad_next) {
  235.                 delivery_setall (int_Qmgr_status_negativeDR);
  236.                 PP_LOG (LLOG_EXCEPTIONS, ("problem with user"));
  237.                 set_1dr (qp, ap -> ad_no,
  238. #ifndef PP52
  239.                      this_msg,
  240. #endif
  241.                      DRR_UNABLE_TO_TRANSFER,
  242.                      DRD_UNRECOGNISED_OR,
  243.                      "problem with recipient");
  244.                 ap -> ad_resp = 0;
  245.             }
  246.             if (rp_isbad(wr_q2dr (qp, this_msg)))
  247.                 delivery_resetDRs (int_Qmgr_status_messageFailure);
  248.             return;
  249.         }
  250.         else if (random () % 50 < 10) {
  251.             PP_LOG (LLOG_EXCEPTIONS, ("problem with message"));
  252.             delivery_setallstate (int_Qmgr_status_messageFailure,
  253.                           "problem with message");
  254.             return;
  255.         }
  256.         else if (random() % 50 < 10) {
  257.             PP_LOG (LLOG_EXCEPTIONS, ("problem with MTA"));
  258.             delivery_setallstate (int_Qmgr_status_mtaFailure, "MTA missing");
  259.             return;
  260.         }
  261.         else if (random() % 50 < 10) {
  262.             PP_LOG (LLOG_EXCEPTIONS, ("problem with message & MTA"));
  263.             delivery_setallstate (int_Qmgr_status_mtaAndMessageFailure,
  264.                           "MTA died");
  265.             return;
  266.         }
  267.     }
  268.  
  269.     for (ap = ad_list; ap; ap = ap -> ad_next) {
  270.         if (ap -> ad_resp) {
  271.             if (ap -> ad_usrreq == AD_USR_CONFIRM ||
  272.                 ap -> ad_mtarreq == AD_MTA_CONFIRM ||
  273.                 ap -> ad_mtarreq == AD_MTA_AUDIT_CONFIRM) {
  274.                 set_1dr (qp, ap -> ad_no,
  275. #ifndef PP52
  276.                      this_msg,
  277. #endif
  278.                      DRR_NO_REASON, -1, NULLCP);
  279.                 delivery_set (ap -> ad_no,
  280.                           int_Qmgr_status_positiveDR);
  281.             }
  282.             else {
  283.                 PP_NOTICE (("address OK"));
  284.                 (void) wr_ad_status (ap, AD_STAT_DONE);
  285. #ifndef PP52
  286.                 (void) wr_stat (ap, qp, this_msg, data_bytes);
  287. #endif
  288.                 delivery_set (ap -> ad_no,
  289.                           int_Qmgr_status_success);
  290.             }
  291.         }
  292.     }
  293.     if (rp_isbad(wr_q2dr (qp, this_msg)))
  294.         delivery_resetDRs(int_Qmgr_status_messageFailure);
  295.     PP_NOTICE ((">>> Message %s transfered",
  296.             this_msg));
  297. }       
  298.             
  299. do_random ()
  300. {
  301.     switch (random() % 5) {
  302.         case 0:
  303.         do_sleep (random()%60);
  304.         break;
  305.         case 2:
  306.         mess_with_network ();
  307.         break;
  308.         case 3:
  309.         cpu_intensive ();
  310.         break;
  311.         case 4:
  312.         mem_intensize ();
  313.         break;
  314.         default:
  315.         mess_with_files (RANDFILE());
  316.         break;
  317.     }
  318. }
  319. do_sleep (secs)
  320. int secs;
  321. {
  322.     PP_NOTICE (("sleeping %d", secs));
  323.     sleep (secs);
  324. }
  325.  
  326. mess_with_files (infile)
  327. char *infile;
  328. {
  329.     char *outfile;
  330.     FILE *fp, *fpo;
  331.     int n;
  332.  
  333.     if ((fp = fopen (infile, "r")) == NULL) {
  334.         PP_SLOG (LLOG_EXCEPTIONS, infile, ("Can't open "));
  335.         return;
  336.     }
  337.     if (random() % 2 == 0) {
  338.         outfile = "/dev/null";
  339.         if ((fpo = fopen (outfile, "w")) == NULL ) {
  340.             PP_SLOG (LLOG_EXCEPTIONS, infile, ("Can't open "));
  341.             fclose (fp);
  342.             return;
  343.         }    
  344.         PP_NOTICE (("mess_with_files %s->/dev/null", infile));
  345.     }
  346.     else {
  347.         PP_NOTICE (("mess_with_files %s -> temp", infile));
  348.  
  349.         fpo = tmpfile ();
  350.     }
  351.         
  352.     n = copy (fp, fpo);
  353.     PP_NOTICE (("copied %d bytes", n));
  354.     (void) fclose (fp);
  355.     (void) fclose (fpo);
  356. }
  357.  
  358.  
  359. mess_with_network ()
  360. {
  361.     switch (random() % 3) {
  362.         case 0:
  363.         do_echo (RANDFILE());
  364.         break;
  365.         case 1:
  366.         do_sink (RANDFILE());
  367.         break;
  368.         default:
  369.         do_chargen ();
  370.         break;
  371.     }
  372. }
  373.  
  374. jmp_buf jmpbuf;
  375.  
  376. static SFD alarmed()
  377. {
  378.     longjmp (jmpbuf, 1);
  379. }
  380.  
  381. do_echo (infile)
  382. char *infile;
  383. {
  384.     FILE *fp;
  385.     FILE *fpi;
  386.     char buf[BUFSIZ];
  387.     int n;
  388.     int total = 0;
  389.     SFP pstat;
  390.  
  391.     PP_NOTICE (("do echo of %s", infile));
  392.     if ((fp = serv_open ("echo")) == NULL)
  393.         return;
  394.     if ((fpi = fopen (infile, "r")) == NULL) {
  395.         PP_SLOG (LLOG_EXCEPTIONS, infile, ("Can't open "));
  396.         fclose (fp);
  397.         return;
  398.     }
  399.     pstat = signal (SIGALRM, alarmed);
  400.     if (setjmp (jmpbuf) == 0) {
  401.         alarm (60);
  402.         while ((n = fread (buf, 1, sizeof buf, fpi)) > 0) {
  403.             if (fwrite (buf, 1, n, fp) != n) {
  404.                 PP_SLOG (LLOG_EXCEPTIONS, "error", ("write"));
  405.                 break;
  406.             }
  407.             total += n;
  408.             if (fread (buf, 1, n, fp) != n)
  409.                 PP_LOG (LLOG_EXCEPTIONS, ("copy back failed"));
  410.         }
  411.         alarm(0);
  412.     }
  413.     PP_NOTICE (("Echoed %d bytes", total));
  414.     (void) fclose (fpi);
  415.     (void) fclose (fp);
  416.     signal(SIGALRM, pstat);
  417. }
  418.  
  419. do_sink (infile)
  420. char *infile;
  421. {
  422.     FILE *fp, *fpi;
  423.     int n;
  424.  
  425.     PP_NOTICE (("do_sink of %s", infile));
  426.     if ((fp = serv_open ("sink")) == NULL)
  427.         return;
  428.  
  429.     if ((fpi = fopen(infile, "r")) == NULL) {
  430.         PP_SLOG (LLOG_EXCEPTIONS, infile, ("Can't open "));
  431.         fclose (fp);
  432.         return;
  433.     }
  434.     n = copy (fpi, fp);
  435.     PP_NOTICE (("Sunk %d bytes", n));
  436.     (void) fclose (fpi);
  437.     (void) fclose (fp);
  438. }
  439.  
  440. do_chargen ()
  441. {
  442.     FILE *fp;
  443.     char buf[BUFSIZ];
  444.     int i;
  445.     int n;
  446.     int total = 0;
  447.     int iter = random () % 2000;
  448.  
  449.     PP_NOTICE (("do_chargen %d buffers", iter));
  450.     if ((fp = serv_open ("chargen")) == NULL)
  451.         return;
  452.  
  453.     for (i = iter; i > 0; i--) { 
  454.         if ((n = fread (buf, 1, sizeof buf, fp)) <= 0)
  455.             break;
  456.         total += n;
  457.     }
  458.     (void) fclose (fp);
  459.     PP_NOTICE (("Received %d bytes", total));
  460. }
  461.     
  462. static int copy (fp1, fp2)
  463. FILE *fp1, *fp2;
  464. {
  465.     char buf[BUFSIZ];
  466.     int total = 0;
  467.     int n;
  468.  
  469.     while ((n = fread (buf, 1, sizeof buf, fp1)) > 0) {
  470.         if (fwrite (buf, 1, n, fp2) != n) {
  471.             PP_SLOG (LLOG_EXCEPTIONS, "write", ("problems with "));
  472.             return NOTOK;
  473.         }
  474.         total += n;
  475.     }
  476.     if (n == 0)
  477.         return total;
  478.     PP_SLOG (LLOG_EXCEPTIONS, "read", ("problems with "));
  479.     return NOTOK;
  480. }
  481.  
  482.             
  483.  
  484. static FILE *serv_open (name)
  485. char *name;
  486. {
  487.     struct servent *sp;
  488.     struct sockaddr_in s_in;
  489.     struct hostent *hp;
  490.     int sd;
  491.  
  492.     if ((sp = getservbyname (name, "tcp")) == NULL) {
  493.         PP_LOG (LLOG_EXCEPTIONS, ("No such service %s", name));
  494.         return NULL;
  495.     }
  496.  
  497.     if ((sd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
  498.         PP_SLOG (LLOG_EXCEPTIONS, "socket", ("Can't create"));
  499.         return NULL;
  500.     }
  501.     s_in.sin_family = AF_INET;
  502.     s_in.sin_port = sp -> s_port;
  503.  
  504.     if ((hp = find_host ()) == NULL) {
  505.         close (sd);
  506.         return NULL;
  507.     }
  508.         
  509.         
  510.     bcopy (hp -> h_addr, (char *)&s_in.sin_addr, hp -> h_length);
  511.     if (connect (sd, &s_in, sizeof s_in) < 0) {
  512.         PP_SLOG (LLOG_EXCEPTIONS, "connect", ("Can't "));
  513.         close (sd);
  514.         return NULL;
  515.     }
  516.  
  517.     return fdopen (sd, "r+");
  518. }
  519.  
  520. static char *host_list[500] = {
  521.     "localhost"
  522.     };
  523. static int n_hosts = 1;
  524. #define RANDHOST() (host_list[random() % n_hosts])    
  525.  
  526. static struct hostent *find_host ()
  527. {
  528.     static int inited = 0;
  529.     char    *host;
  530.     static void init_hosts ();
  531.     int i;
  532.     struct hostent *hp;
  533.  
  534.     if (!inited) {
  535.         init_hosts ();
  536.         inited ++;
  537.     }
  538.     for (i = 0;i < 20; i++) {
  539.         if ((hp = gethostbyname (host = RANDHOST())) == NULL)
  540.             continue;
  541.         PP_NOTICE (("Using host %s", host));
  542.         return hp;
  543.     }
  544.     return NULL;
  545. }
  546.  
  547. static void init_hosts ()
  548. {
  549.     char path[BUFSIZ];
  550.     extern char *tbldfldir;
  551.     FILE *fp;
  552.     char *cp;
  553.  
  554.     getfpath (tbldfldir, "hosts", path);
  555.  
  556.     if ((fp = fopen (path, "r")) == NULL)
  557.         return;
  558.     while (fgets(path, sizeof path, fp)) {
  559.         if ((cp = index(path, '\n')) != NULL)
  560.             *cp = 0;
  561.         host_list[n_hosts++] = strdup (path);
  562.     }
  563.     fclose (fp);
  564. }
  565.  
  566. static double fact(n)
  567. int n;
  568. {
  569.     if (n <= 1)
  570.         return 1;
  571.     return fact(n-1) * (double)n;
  572. }
  573.  
  574. cpu_intensive ()
  575. {
  576.     int niter = random()%1000;
  577.     int i;
  578.     double result = 1.0;
  579.  
  580.     PP_NOTICE (("working out e with %d iterations", niter));
  581.     /* work out e for now */
  582.     for (i = 1; i < niter; i++) {
  583.         result = result + (double)1 / fact(i);
  584.     }
  585.     PP_NOTICE (("result e is %.20lf", result));
  586. }
  587.  
  588.  
  589. mem_intensize ()
  590. {
  591.     int ps = getpagesize ();
  592.     int npages = random() % 100;
  593.     char **pages;
  594.     int i, j;
  595.     char c;
  596.  
  597.     PP_NOTICE (("memory stuff based on %d pages", npages));
  598.     pages = (char **) calloc(npages, sizeof (char **));
  599.     for (i = 0; i < npages; i++)
  600.         pages[i] = malloc (ps);
  601.     for (j = random() % 100; j > 0; j--) 
  602.         for (i = 0; i < npages; i++) {
  603.             c = pages[i][0] % 4;
  604.             fact(c);
  605.         }
  606. }
  607.